<script setup>
import * as THREE from 'three'
import {OrbitControls} from 'three/examples/jsm/controls/OrbitControls'
import {ref,onMounted} from 'vue'
let firstThree = ref()
const scene = new THREE.Scene()

// const geometry = new THREE.BoxGeometry(100,100,100)
const geometry = new THREE.SphereGeometry(50,6,5)
// 缩放
geometry.scale(2,2,3)
geometry.translate(20,50,100)
geometry.rotateZ(45)
// 几何体顶点居中，会直接覆盖translate属性
geometry.center()
// 线模型所使用的点材质

// wireframe属性为true，只绘制几何体的结构，不填充
const metaerial = new THREE.MeshLambertMaterial({
  color:0x00ffff,
  side:THREE.DoubleSide,
  wireframe:true
})

const line = new THREE.Mesh(geometry, metaerial); 
line.position.set(0,0,0)
scene.add(line)


// 环境光
const ambient = new THREE.AmbientLight("#fff",0.8)
scene.add(ambient)

const width = window.innerWidth;
const height = window.innerHeight;

const camera = new THREE.PerspectiveCamera(100,width/height,1,8000)
camera.position.set(100,100,200)
camera.lookAt(0,0,0) 

// 解决渲染出来的图像有锯齿
const renderer = new THREE.WebGLRenderer({
  antialias:true, // 启动抗锯齿
});
renderer.setSize(width,height)
// 设置屏幕像素比
renderer.setPixelRatio(window.devicePixelRatio)
renderer.render(scene,camera)

const controls = new OrbitControls(camera,renderer.domElement)
controls.addEventListener("change",function(){
  // 重新渲染，必须有这步操作，否则数据改变，但无法看到
  renderer.render(scene,camera)
})
onMounted(()=>{
  document.getElementById("firstThree").appendChild(renderer.domElement)
})

</script>

<template>
  <div id="firstThree" ref="firstThree"></div>
</template>

<style scoped>
  #firstThree{
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    position: absolute;
  }
</style>
